package com.aigou.service;

import com.aigou.dao.UserMapper;
import com.aigou.exception.AigouException;
import com.aigou.pojo.User;
import com.aigou.pojo.UserExample;
import com.aigou.utils.MD5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    public PageInfo<User> findAll(Integer page
            , Integer size, User user) {
        PageHelper.startPage(page, size);
        UserExample ex = new UserExample();
        UserExample.Criteria criteria = ex.createCriteria();
        criteria.andIsDeleteEqualTo(0);
        if (!StringUtils.isEmpty(user.getUsername())) {
            criteria.andUsernameLike("%" +user.getUsername()+ "%");
        }
        return new PageInfo<User>(userMapper.selectByExample(ex));
    }

    public User findOne(Integer id) {
        if (id == null) {
            throw new AigouException("请输入id");
        }
        User user = userMapper.selectByPrimaryKey(id);
        if (user.getIsDelete().equals(1)) {
            throw new AigouException("用户不存在");
        }
        return user;
    }

    public void add(User user) {
        if (StringUtils.isEmpty(user.getUsername())) {
            throw new AigouException("请输入用户名");
        }
        if (user.getUsername().length() > 20) {
            throw new AigouException("用户名太长");
        }
        if (StringUtils.isEmpty(user.getPassword())) {
            throw new AigouException("请输入密码");
        }

        UserExample ex = new UserExample();
        UserExample.Criteria criteria = ex.createCriteria();
        criteria.andUsernameLike(user.getUsername());
        criteria.andIsDeleteEqualTo(0);
        List<User> userList = userMapper.selectByExample(ex);
        if (userList.size() > 0) {
            throw new AigouException("用户已存在");
        }

        user.setIsDelete(0);
        Date date = new Date();
        user.setCreateTime(date);
        user.setUpdateTime(date);
        user.setStatus(0);
        user.setPassword(MD5Util.getMd5(user.getPassword()));
        userMapper.insertSelective(user);
    }

    public User login(String username, String password) {
        UserExample ex = new UserExample();
        UserExample.Criteria criteria = ex.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andIsDeleteEqualTo(0);
        List<User> userList = userMapper.selectByExample(ex);
        if (userList.size() == 0) {
            throw new AigouException("用户或密码错误");
        }
        User dbUser = userList.get(0);

        if (!dbUser.getPassword().equals(MD5Util.getMd5(password))){
            throw new AigouException("用户或密码错误");
        }
        dbUser.setPassword(null);
        return dbUser;
    }

    public void update(User user) {
        if (user.getId() == null) {
            throw new AigouException("请输入Id");
        }
        if (StringUtils.isEmpty(user.getUsername())) {
            throw new AigouException("请输入用户名");
        }
        if (StringUtils.isEmpty(user.getPassword())) {
            throw new AigouException("请输入密码");
        }

        UserExample ex = new UserExample();
        UserExample.Criteria criteria = ex.createCriteria();
        criteria.andUsernameLike(user.getUsername());
        criteria.andIsDeleteEqualTo(0);
        List<User> userList = userMapper.selectByExample(ex);
        if (userList.size() > 0) {
            User dbUser = userList.get(0);
            if (!dbUser.getId().equals(user.getId())) {
                throw new AigouException("用户已存在");
            }
        }


        Date date = new Date();
        user.setUpdateTime(date);
        userMapper.updateByPrimaryKeySelective(user);
    }

    public void delete(Integer id) {
        User user = new User();
        Date date = new Date();
        user.setUpdateTime(date);
        user.setId(id);
        user.setIsDelete(1);
        userMapper.updateByPrimaryKeySelective(user);
    }

}
